package org.jeecg.modules.simulator.model.train.manager;

import org.jeecg.modules.simulator.model.train.action.*;
import org.jeecg.modules.simulator.model.train.event.SimpleActionRecorderEvent;
import org.jeecg.modules.simulator.model.train.event.msg.SimpleActionRecorderMsg;
import org.jeecg.modules.simulator.model.train.timeplan.TrainPlanTable;
import org.jeecg.modules.simulator.util.SimulatorParamStr;
import org.jeecg.modules.simulator.util.SpringUtil;

import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @program: simulator-b20240918
 * @description:
 * @author: renzhg
 * @create: 2024-10-04 13:28
 **/
public class PlanTableManager {


    /**
     * @description: 创建简单发车时刻表
     *
     * @author:  renzhg
     * @date:  2024/9/18 15:58
     **/
    public void createSimplePlanTable(TrainPlanTable firstTrainPlanTable, long planSpeed,String now){
//        String lineid="1";
//        String trainid="1";
//        String trainNum="2";
//        String arriveTime="15:38";
//        String startStationId="0135";
//        String startStationName="南京南";
//        String departTime="15:40";
//        Integer index=1;
//        Integer remainTime=30;
//第一个时刻表，表示发车站点，和发车时间
        //根据给定的创建列车动作记录表
        TrainActionRecorders trainActionRecorders = new TrainActions();
        String key=firstTrainPlanTable.getTrainId()+"-"+firstTrainPlanTable.getTrainNum();
        trainActionRecorders.setRocordersId(key);
        trainActionRecorders.setLineId(firstTrainPlanTable.getLineId());
        Map<Integer, TrainActionRecorder> trainActionRecorderMap = new LinkedHashMap<>();

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");
        try {
            TrainActionRecorder trainActionRecorder = new TrainAction(firstTrainPlanTable.getArriveStaionName(), SimulatorParamStr.ACTION_STOP);
            trainActionRecorder.setStartTime(LocalTime.parse(firstTrainPlanTable.getArriveTime(), formatter));
            trainActionRecorder.setEndTime(LocalTime.parse(firstTrainPlanTable.getDepartTime(), formatter));
            trainActionRecorder.setAffiliateType(SimulatorParamStr.ACTION_DOCKING);

            trainActionRecorder.setExecutionStatus(ExecutionStatus.initialize);
            trainActionRecorder.setLast(false);

            TrainActionRecorder trainActionRecorder2 = new TrainAction(firstTrainPlanTable.getArriveStaionName()+"_"+"end", SimulatorParamStr.ACTION_RUN);
            trainActionRecorder2.setStartTime(LocalTime.parse(firstTrainPlanTable.getDepartTime(), formatter));
            trainActionRecorder2.setExecutionStatus(ExecutionStatus.initialize);
            trainActionRecorder2.setLast(false);
            trainActionRecorder2.setPlanSpeed(planSpeed);
            trainActionRecorder2.setActualSpeed(planSpeed);

            trainActionRecorderMap.put(firstTrainPlanTable.getIndex(), trainActionRecorder);
            trainActionRecorderMap.put(firstTrainPlanTable.getIndex()+1, trainActionRecorder2);
            trainActionRecorders.setTrainActionRecordMap(trainActionRecorderMap);
            trainActionRecorders.setStatus(ActionRecordersStatus.nostart);
            //设置简单加车类型
            trainActionRecorders.setTrainActionRecordersType(SimulatorParamStr.ACTION_RECORDERS_TYPE_SIMPLE);
            SimpleActionRecorderMsg simpleActionRecorderMsg = new SimpleActionRecorderMsg();
            simpleActionRecorderMsg.setTrainActionRecorders(trainActionRecorders);

            LocalTime localTime = LocalTime.parse(now);
            simpleActionRecorderMsg.setEventTime(localTime);

            simpleActionRecorderMsg.setLineId(firstTrainPlanTable.getLineId());
            simpleActionRecorderMsg.setStationId(firstTrainPlanTable.getArrivateStaionId());
            simpleActionRecorderMsg.setKey(key);
            SpringUtil.getApplicationContext().publishEvent(new SimpleActionRecorderEvent(simpleActionRecorderMsg));

        } catch (DateTimeParseException e) {
            e.printStackTrace();
            System.out.println("Invalid date-time format: " + e.getMessage());
        }


    }
}
